import { createStore } from 'vuex'
import db from '../utils/storage';
import axios from '../utils/axios';

export default createStore({
  state: {
    token: db.ss.get('token') || null,
    userInfo: db.ss.get('userInfo') || null,
    init: db.ss.get('init') || null,
    menu: [],
    product_category:[],
    roleMenus:db.ss.get('roleMenu') || [],
    configuration: db.ss.get('configuration') || null,
    isLoading: false
  },
  mutations: {
    SET_TOKEN(state, data): void {
      db.ss.set('token', data);
      state.token = data;
    },
    SET_USERROLEMENUS(state, data): void {
      db.ss.set('roleMenu', data);
      state.roleMenus = data;
    },
    SET_USERINFO(state, data): void {
      db.ss.set('userInfo', data);
      state.userInfo = data;
    },
    SET_CONFIGURE(state, data): void {
      db.ss.set('configuration', data);
      state.configuration = data;
    },
    SET_INIT(state, data): void {
      db.ss.set('init', data);
      state.init = data;
    },
    SET_MENU(state, data): void {
      state.menu = data;
    },
    SET_PRODUCT_CATEGORY(state, data): void {
      state.product_category = data;
    },
    CHANGE_LOAD(state, data): void {
      state.isLoading = data;
    },
    CLEAR_CACHE(state): void {
      state.token = '';
      db.ss.clear();
    }
  },
  actions: {
    SET_TOKEN({ commit }, data): void {
      commit('SET_TOKEN', data);
    },
    SET_USERROLEMENUS({ commit }, data): void {
      commit('SET_USERROLEMENUS', data);
    },
    GET_CONFIGURE({ commit }): Promise<any> {
      return new Promise((resolve, reject) => {
        axios
          .get('/public/getConfiguration')
          .then((res: any) => {
            commit('SET_CONFIGURE', res.Data);
            resolve(res.Data);
          })
          .catch((error) => reject(error));
      });
    },
    GET_USERINFO({ commit }): Promise<any> {
      return new Promise((resolve, reject) => {
        axios
          .get('/User/GetLoginUserInfo')
          .then((res: any) => {
            commit('SET_USERINFO', res.Data);
            resolve(res.Data);
          })
          .catch((error) => reject(error));
      });
    },
    SET_INIT({ commit }, data) {
      commit('SET_INIT', data);
    },
    CHANGE_LOAD({ commit }, data) {
      commit('CHANGE_LOAD', data);
    },
    GET_MENU({ state, commit }, update): Promise<any> {
      return new Promise((resolve, reject) => {
        if (state.menu.length > 0 && !update) {
          resolve(state.menu);
        } else {
          axios
            .get('/Menu/List')
            .then((res: any) => {
              commit('SET_MENU', res.Data);
              resolve(res.Data);
            })
            .catch((error) => reject(error));
        }
      });
    },
    GET_PRODUCT_CATEGORY({ state, commit }, params): Promise<any> {
      // console.log("GET_PRODUCT_CATEGORY",update)
      return new Promise((resolve, reject) => {
        if (state.product_category.length > 0 && !params) {
          resolve(state.product_category);
        } else {
          axios
            .get('/ProductCategory/List',params)
            .then((res: any) => {
              commit('SET_PRODUCT_CATEGORY', res.Data);
              resolve(res.Data);
            })
            .catch((error) => reject(error));
        }
      });
    }
  },
  modules: {}
});
